// void, Obj This

Unit this, u;
Settlement sett;
Building bld;
int i, uc, l;
Item item;
point ptGo;
str itemName;

Sleep(1120+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsUnit;  

while(!.IsValid) Sleep(10000);

uc = 0;
l = 0;
while(.IsValid){
	Sleep(2000);
	
	itemName = "tax_collector_stop";
	if(.InHolder){
		if(.AsHero.army.count == .AsHero.maxarmy)
		if(.health == .maxhealth){
			bld = .GetHolderSett.GetCentralBuilding;
			if(bld.IsHeirOf("Outpost")){
				Unit u;
				sett = .GetHolderSett;
				
				for(i=0; i<sett.UnitsCount; i+=1){
					u = sett.Units[i].AsUnit;
					
					if(u.IsHeirOf("TaxCollector"))
						if(u != this){
							//pr("in holder");
							ptGo.Set(bld.radius,bld.radius);
							u.SetCommand("move", u.posRH+ptGo);
							//u.SetEntering(false);
						}
					
				}
				//pr("powinien zarabiac");
				//pr("uc: "+uc);
				if(uc > 0){
					sett.SetGold(sett.gold+uc);
					sett.GoldConverted(uc);
				}
				
				itemName = "tax_collector";
				
				if(l < 5000) l+=1;
			}		
		}
	}
	if(.item_count > 0){
		item = .GetItem(1);
		if(item.IsValid){
			int suc;
			
			if(item.name != itemName){
				.ExchangeItem(item, itemName);
			}
			
			if(item.name != "tax_collector_stop"){
				
				if(l < 50){
					suc = 1;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 50 && l < 150){
					suc = 2;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 150 && l < 300){
					suc = 3;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 300 && l < 500){
					suc = 4;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 500 && l < 750){
					suc = 5;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 750 && l < 900){
					suc = 6;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 900 && l < 1050){
					suc = 7;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 1050 && l < 1200){
					suc = 8;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 1200 && l < 3000){
					suc = 9;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else if(l >= 3000){
					suc = 10;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				} else {
					suc = 1;
					if(item.use_count != suc)
						item.SetUseCount(suc);
				}
				
				if(.level < suc)
					.SetLevel(suc);
			} 
			
			uc = item.use_count;
		}
	}
}